﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCL.UI.PopWindows
{
    public partial class InspectNote : EFViewCmes
    {
        private string actionType;
        
        public InspectNote(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
            this.LayoutUpdated += new EventHandler(InspectNote_LayoutUpdated);
        }

        /// <summary>
        /// 设置巡检人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InspectNote_LayoutUpdated(object sender, EventArgs e)
        {
            people.eViewInitParams = "bindingService=ZM03.query,columns=empCode|empName,queryConditions=empCode|empName,initParam=orgCode:" + this.fdDepartmentId.Value;
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.actionType = "insert";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.actionType = "update";
                    this.fdNotecode.IsReadOnly = true;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    this.Billing.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 点击确定按钮，工单状态默认填入草拟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            this.fdStatus.Value = "草拟";
            base.OnCommitButtonClick("ZMCL01", sender, e);
        }

        /// <summary>
        /// 点击发单按钮，工单状态默认填入发单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BillingClick(object sender, RoutedEventArgs e)
        {
            this.fdStatus.Value = "发单";
            base.OnCommitButtonClick("ZMCL01", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 重写了确定按钮执行事件的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                if("insert".Equals(actionType))
                {
                    NotifyViewDataChanged(e.EIInfo.Get("noteCode").ToString());
                }
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 巡检人根据巡检单位变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                var code = box.SelectedItem.GetType().GetProperty("orgCode").GetValue(box.SelectedItem, null);
                people.eViewInitParams = "bindingService=ZM03.query,columns=empCode|empName,queryConditions=empCode|empName,initParam=orgCode:" + code;
            }
        }
    }
}
